package com.jiuyin.dianjing.ui.activity.setting;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.helper.ApiHelper;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.ToastUtil;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_CHECK_CODE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_CODE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_MOBILE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NATION_CODE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_NATION_CODE;
import static com.jiuyin.dianjing.app.HelperApplication.getContext;
import static com.jiuyin.dianjing.constant.AppConstant.COUNT_DOWN_TIME_NAME;
import static com.jiuyin.dianjing.constant.AppConstant.COUNT_DOWN_TIME_UNIT;
import static com.jiuyin.dianjing.constant.AppConstant.COUNT_DOWN_VERIFY_CODE_TIME;

/**
 * 修改完成
 */
public class ModifyNewPhoneDoneActivity extends BaseActivity {

    @BindView(R.id.tv_pwd_old)
    TextView mNewPhone;
    @BindView(R.id.tv_send)
    TextView mSend;
    @BindView(R.id.edt_verify_code)
    EditText mVerifyCode;

    private String mPhone;
    private String mCheckCode;//验证识别码
    private CountDownTimer mTimer;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_modify_phone_verify_done;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (null != intent) {
            mPhone = intent.getStringExtra(KEY_MOBILE);
            mNewPhone.setText(getString(R.string.setting_phone_modify_verify_new_phone, mPhone));
        }
    }

    @Override
    protected void initView() {
        setTitle(R.string.setting_phone_modify_done);
    }

    @OnClick({R.id.tv_done, R.id.tv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_done:
                checkSmsCode();
                break;
            case R.id.tv_send://调用发送验证码接口
                sendVerifyCode();
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendVerifyCode() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_NATION_CODE, VALUE_NATION_CODE);
        params.put(KEY_MOBILE, mPhone);
        JSONObject jsonObject = new JSONObject(params);
        String content = jsonObject.toString();
        ApiHelper.getInstance(getContext()).appSendSmsApi(content, data -> {
            setStatus();
            mCheckCode = data;
        });
    }

    private void setStatus() {
        if (null != mTimer) {
            mTimer.cancel();
        }
        mSend.setEnabled(false);
        mTimer = new CountDownTimer(COUNT_DOWN_VERIFY_CODE_TIME, COUNT_DOWN_TIME_UNIT) {
            @Override
            public void onTick(long millisUntilFinished) {
                mSend.setText((millisUntilFinished / COUNT_DOWN_TIME_UNIT + 1) + COUNT_DOWN_TIME_NAME);
            }

            @Override
            public void onFinish() {
                mSend.setEnabled(true);
                mSend.setText(R.string.login_get_verify_code);
            }
        };
        mTimer.start();
    }

    /**
     * 验证验证码
     */
    private void checkSmsCode() {
        String verifyCode = mVerifyCode.getText().toString();
        if (TextUtils.isEmpty(verifyCode)) {
            ToastUtil.showShort(R.string.login_msg_verify_code_hint);
            return;
        }
        if (TextUtils.isEmpty(mCheckCode)) {
            ToastUtil.showShort(R.string.setting_phone_modify_verify_code);
            return;
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_CODE, verifyCode);
        params.put(KEY_CHECK_CODE, mCheckCode);
        JSONObject jsonObject = new JSONObject(params);
        ApiHelper.getInstance(getContext()).appMsgCheckApi(jsonObject.toString(), data -> {
            if (data) {
                HashMap hashMap = new HashMap();
                hashMap.put(KEY_MOBILE, mPhone);
                hashMap.put(KEY_TOKEN, HelperApplication.getToken());
                JSONObject data2 = new JSONObject(hashMap);

                ApiResultCallback requestCallback = new DefaultApiResult() {
                    @Override
                    public void onSuccess(JsonObject data) {
                        if (null != mTimer) {
                            mTimer.cancel();
                        }
                        quitAndLogin();
                    }
                };
                ServerApi.post(ApiEnum.APP_UPDATE_USER_API.getUrl(), data2, null, progress, d -> addDisposable(d), requestCallback);
            }
        });
    }
}
